/*
 * @package	COREJS
 */

var COREJS = {	
	cbDialog : function(sz_Message){
		$('#cb-dialog').dialog({
			modal: true,
			bgiframe: true,			
			title: 'Message',
			resizable: false,
			autoOpen: false,
			close: function() {
			}
		});
		$('#cb-dialog-content').html(sz_Message);
		$('#cb-dialog').dialog('open');
	},
	/**
	 * Initialize diaglog
	 */
	initDialog : function(the_Div_Id, width, height){
		the_sz_DialogId = the_Div_Id;
		if($('#'+the_sz_DialogId).length == 0) {
			$('body').append("<div id='" + the_sz_DialogId + "' class='hide'></div>");
		}
		$('#'+the_sz_DialogId).dialog({
			autoOpen: false,
			bgiframe: true,
			modal: true,
			resizable: false,
			height: height,
			width: width
		});
	},
	/**
	 * Show OK dialog
	 */
	v_fShowOKDialog : function(the_sz_Message, callBackOK){
		COREJS.initDialog('OK_dialog', 300, 150);
		$('#OK_dialog').dialog('option', 'title', "Thông tin");
		$('#OK_dialog').html('<br />'+the_sz_Message);
		$('#OK_dialog').dialog('option', 'buttons', { 
			"Đóng lại" : function() {
				$('#OK_dialog').dialog('close');
				if (callBackOK) {
					callBackOK();
				}
				$('#OK_dialog').dialog('close');
			}
		});		
		$('#OK_dialog').dialog('open').parent().css({position:"fixed"});
		return true;
	},
	
	v_fShowStatusDialog : function(the_sz_Message){
		COREJS.initDialog('Status_dialog', 300, 150);
		$('#Status_dialog').dialog('option', 'title', "Thông tin");
		$('#Status_dialog').dialog("option", "closeOnEscape", false);
		$('#Status_dialog').html('<br />'+the_sz_Message);
		$('#Status_dialog').dialog('open').parent().css({position:"fixed"});
		return true;
	},
	
	/**
	 * Show Yes - No dialog
	 */
	v_fShowYesNoDialog : function(the_sz_Message, callBackYes, callBackNo){
		COREJS.initDialog('confirm_diag', 300, 150);
		$('#confirm_diag').dialog('option', 'title', COREJS._getJsLang('_CONFIRM'));
		$('#confirm_diag').html('<br />' + the_sz_Message);
		$('#confirm_diag').dialog('option', 'buttons', 
			{ 
				"Không": function() {
					if (callBackNo){
						callBackNo();
						$(this).dialog('close');
					}
					$(this).dialog('close');
				},
				"Có": function() {
					if(callBackYes){
						callBackYes();
						$(this).dialog('close');
					}
					$(this).dialog('close');
				}
			});

		$('#confirm_diag').dialog('open').parent().css({position:"fixed"});	
		
		$('#confirm_diag').css('font-size', '12px');
		$('.ui-dialog-title').css('font-size', '12px');
		$('.ui-dialog-buttonpane button').css('color', '#FFF');
		$('.ui-dialog-buttonpane button').css('width', '65px');
		$('.ui-dialog-buttonpane button').css('font-size', '12px');
		$('.ui-dialog-buttonpane button').css('padding', '0.1em 0.6em 0.1em');
		$('.ui-dialog-buttonpane button:nth-child(1)').css('background', '-moz-linear-gradient(center top , #00ADEE, #0078A5) repeat scroll 0 0 transparent');
		$('.ui-dialog-buttonpane button:nth-child(2)').css('background', '-moz-linear-gradient(center top , #FAA51A, #F47A20) repeat scroll 0 0 transparent');
		return true;
	},
	
	/**
	 * Show information dialog
	 */
	v_fShowInforDialog : function(the_sz_Message, callBackOK){
		COREJS.initDialog();
		$('#dialog-container').dialog('option', 'title', "Information");
		$('#dialog-container').text(the_sz_Message);
		$('#dialog-container').dialog('option', 'buttons', { 
			"OK" : function() {
				if (callBackOK) {
					callBackOK();
				}
				$(this).dialog('close');
			}
		});		
		$('#dialog-container').dialog({
			height: 400,
			width: 600
		});
		$('#dialog-container').dialog('open').parent().css({position:"fixed"});
	},
	
	/**
	 * Display inpage popup
	 * @param		none
	 * @return		void
	 */
	v_fShowInpagePopup : function(the_sz_Div, the_sz_Title, width, height, callBackOK, callBackCalcel){
		this.initDialog(the_sz_Div, width, height);
		$('#'+the_sz_Div).dialog('option', 'title', the_sz_Title);
		$('#'+the_sz_Div).dialog('option', 'buttons', { 
			"Cancel" : function() {
				if (callBackOK) {
					callBackOK();
				}
				$(this).dialog('close');
			},
			"Ok" : function() {
				if (callBackCalcel) {
					callBackCalcel();
				}
				//$(this).dialog('close');
			},			
			
		});		
		$('#'+the_sz_Div).dialog('open').parent().css({position:"fixed"});		
	},
	
	/**
	 * Display Form popup
	 * @param		none
	 * @return		void
	 */
	v_fShowFromPopup : function(the_sz_Div, the_sz_Title, width, height){
		this.initDialog(the_sz_Div, width, height);
		$('#'+the_sz_Div).dialog('option', 'title', the_sz_Title);
		/*
		$('#'+the_sz_Div).dialog('option', 'buttons', { 
			"OK" : function() {
				if (callBackCalcel) {
					callBackCalcel();
				}
				//$(this).dialog('close');
			},
			"Cancel" : function() {
				if (callBackCalcel) {
					callBackOK();
				}
				$(this).dialog('close');
			},			
			
		});		
		*/
		$('#'+the_sz_Div).dialog('open').parent().css({position:"fixed"});		
	},	
	
	/**
	 * Display Form popup
	 * @param		none
	 * @return		void
	 */
	v_fShowFromPopupWithButton : function(the_sz_Div, the_sz_Title, width, height, callBackOK){
		this.initDialog(the_sz_Div, width, height);
		$('#'+the_sz_Div).dialog('option', 'title', the_sz_Title);
		
		$('#'+the_sz_Div).dialog('option', 'buttons', { 
			"OK" : function() {
				if (callBackOK) {
					callBackOK();
				}
				//$(this).dialog('close');
			}
		});		
		
		$('#'+the_sz_Div).dialog('open').parent().css({position:"fixed"});		
	},		
	
	v_fShowSingleViewPopup : function(the_sz_Div, the_sz_Title, width, height, callBackClose){
		this.initDialog(the_sz_Div, width, height);
		$('#'+the_sz_Div).dialog('option', 'title', the_sz_Title);
		
		$('#'+the_sz_Div).dialog('option', 'buttons', { 
			"Close" : function() {
				if (callBackClose) {
					callBackClose();
				}
				$(this).dialog('close');
			}		
			
		});		
		
		$('#'+the_sz_Div).dialog('open').parent().css({position:"fixed"});		
	},	
	
	/**
	 * Call ajax to get Json
	 * @param 	String the_sz_Url
	 * @param 	the_a_Data
	 * @param 	fnCallBackOK
	 * @return	none
	 */	
	
	v_fGetJson : function(the_sz_Url, the_a_Data, fnCallBackOK)
	{
		$.ajax(
				{
					type: 'GET',
					url: the_sz_Url,
					dataType: "json",
					data: the_a_Data,
					success: function(msg){
						if(msg != null && msg.errors != null) {
							/*alert('Error: ' + msg.message);*/
						}
						else{
							if (fnCallBackOK) {
								fnCallBackOK(msg);
							}
						}
						$('#system-busy').css("display", "none");
					},
					error: function(xhr, ajaxOptions, thrownError) {
						/*alert('Error when call ajax!');*/
						//$('#system-busy').css("display", "none");
						//COREJS.v_fShowOKDialog('Timeout or connection error!');
					}
					//timeout: 100000
				});		
	},	
	
	/**
	 * Get javascript tranlsation
	 * 
	 * @param	string the_Key
	 * @return	string	Translated text
	 */
	_getJsLang : function(the_Key) {
		if(the_Key != ''){
			objTrans = JSTRAN.language;
			return objTrans[the_Key];
		}
	},
	
	/**
	 * Get base URL for javascript
	 * 
	 * @param	string the_Key
	 * @return	string	Translated text
	 */	
	_getBaseUrl : function() {
		return COREJS._getJsLang('_GENCRM_URL');
	},
	
	/**
	 * Remove an element from page
	 * @param	none
	 * @return	void
	 */
	v_fRemove : function(the_Element) {
		$('#'+the_Element).remove();
	},
	
	/**
	 * Split a string by string (Similar PHP "explode" function)
	 * 
	 * @param	string delimeter
	 * @param	string string
	 * @return	array
	 */
	explode : function(delimeter, string){
		return string.split(delimeter);
	},
	
	/**
	 * Checks if a value exists in an array (Similar PHP "in_array" function) 
	 * @param	mixed needle
	 * @param	array haystack
	 * @return	boolean
	 */
	in_array : function(needle, haystack) {
		return $.inArray(needle, haystack);
	},
	
	v_fChangeButtons : function()
	{
		// change text of buttons
		if ($('.ui-dialog-buttonpane button:nth-child(1)').text() == 'OK') {
			$('.ui-dialog-buttonpane button:nth-child(1)').text('Lưu lại');
		}
		if ($('.ui-dialog-buttonpane button:nth-child(2)').text() == 'Cancel') {
			$('.ui-dialog-buttonpane button:nth-child(2)').text('Hủy bỏ');
			$('.ui-dialog-buttonpane button').css('color', '#FFF');
			$('.ui-dialog-buttonpane button').css('width', '65px');
			$('.ui-dialog-buttonpane button').css('font-size', '12px');
			$('.ui-dialog-buttonpane button').css('padding', '0.1em 0.6em 0.1em');
			$('.ui-dialog-buttonpane button:nth-child(1)').css('background', '-moz-linear-gradient(center top , #00ADEE, #0078A5) repeat scroll 0 0 transparent');
			$('.ui-dialog-buttonpane button:nth-child(2)').css('background', '-moz-linear-gradient(center top , #FAA51A, #F47A20) repeat scroll 0 0 transparent');			
		}		
	},
	
	/* ------------------------ MESSENGER SYSTEM ONLY FOR MAIL--------------------------- */
	AlertMsg : function(msg){
		 if($(".msg-sys-mail").html()==null)
			{
				$('body').append('<div class=\"msg-sys-mail\">'+msg+'</div>');
			}else{
				$(".msg-sys-mail").html(msg);
			}
		$(".msg-sys-mail").show();
	},
	CloseMsg : function(){
		$(".msg-sys-mail").remove();
	},
	v_fDisplayDialog : function(the_DivId) {
		$("#"+the_DivId).dialog("open");
		$('#'+the_DivId).dialog({
			height: 400,
			width: 600,
		});
		$("#"+the_DivId).css("padding", "0px");		
	},
	/* ------------------------ FOR MESSENGER SYSTEM  (END)--------------------------- */
	locationurl:function(url,tab)
	{
		if (tab != 0) {
			window.location = url + $('.pagecombox #paging').val() + '&tab=' + tab;
		}
		else
		{
			window.location = url + $('.pagecombox #paging').val();
		}
	}	
	/* ------------------------ FOR TESTING (END)--------------------------- */	
}

$.extend({
	  getUrlVars: function(){
	    var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	      hash = hashes[i].split('=');
	      vars.push(hash[0]);
	      vars[hash[0]] = hash[1];
	    }
	    return vars;
	  },
	  getUrlVar: function(name){
	    return $.getUrlVars()[name];
	  }
	});